<?php
function loadcategoryproductbyid($id)
{
    global $dbsql,$language;
    
    $sql_select = "select * from tblproduct_category where iId=$id";
    return $dbsql->query_fetch($sql_select);
}

function ListOptionParentProduct($select=0,$idParent=0)
{
global $dbsql,$language,$spaceoption,$act,$id;

	$hasRoot=true;
	if(intval($spaceoption)==0)
	{
		$options="";
		$spaceoption=0;
	}
	if($idParent==-1)
	{
		$idParent=0;
		$hasRoot=false;
	}
	$sql_select="select cName,iId from tblproduct_category where ";
	if($idParent==0)
	{
		$sql_select.=" iParentId=0 ";
	}else
	{
		$sql_select.=" iParentId=$idParent  ";
		if(strpos($act,"edit")!==false)
		{
			$sql_select.=" and iId!=$id";
		}
	}
	$sql_select.=" and cLanguage='$language' order by iOrder";
	
	$rs=$dbsql->query($sql_select);
	while($result=$dbsql->fetch_array($rs))
	{
	   if($act=='edit' && $result['iId']==$id)
       {
            continue;
       }
		$space=str_repeat("&nbsp;",$spaceoption);
		if($hasRoot)
		{
			if($select==$result['iId'])
			{
				$options.="<option value='".$result['iId']."' selected>".$space.$result['cName']."</option>";
			}else
			{
				$options.="<option value='".$result['iId']."'>".$space.$result['cName']."</option>";
			}	
			$spaceoption+=10;
		}		
		$options.=	ListOptionParentProduct($select,$result['iId']);
		if($hasRoot)
		{
			$spaceoption-=10;
		}
	}
	return $options;
}


function getPropertyUpload($idP)
{	
return null;
	$fields=getPropertyProduct($idP);
	if(count($fields)==0)
	{
		$idparent	= getIdParent($idP);
		if($idparent>0)
		{
			$fields=getPropertyProduct($idparent);
		}
	}
	
	foreach($fields as $i=> $field)
	{
		$value	= gpc_getStringPost("property_".$field['id']);
		$fields[$i]['value']=$value;
	}
	return savePropertyProduct($fields);
}


function GetIdCatProduct()
{
global $dbsql,$language;

	$sql_select    = "select iId from tblcategory where cType='Product' and cLanguage='$language'";
	
	$rs            = $dbsql->query($sql_select);
	$result        = $dbsql->fetch_array($rs);
	return $result['iId'];
}


function getPropertyProduct($id)
{
    global $dbsql,$language;
    ImportClass('json');
    $data       = getConfig('ProductP_'.$id);
    $data       = FJSON::decode($data,true);
    return $data;
}


function savePropertyProduct($properties,$id=0)
{	
    global $dbsql,$language;
    ImportClass('json');
    $data       = FJSON::encode($properties,false);
    
    saveConfig('ProductP_'.$id,$data);
}

function Delete_Property($id)
{
}


function BuildTableProduct($fields)
{
	$return='<table width=600 border=1 cellpadding="0" cellspacing="0">';
	foreach($fields as $i=>$field)
	{
		$return.="<tr>";
		$return.="<td width=200>".$field[1]."</td>";
		$return.="<td>&nbsp;</td>";
		$return.="</tr>";
	}
	$return.="</table>";
	return $return;
}

function hasChildProduct($idp)
{
      global $dbsql;
      //-----------------------------------------------------
      $sql_select = "select iId from tblproduct where  iParentId = $idp";
      
      $rs=$dbsql->query($sql_select);
      $rows = $dbsql->num_rows($rs);
      if($rows>0)return true;
      return false;
}


function  ListPropertyFilter($idCat)
{
global $dbsql,$language,$idP,$id;
	
	$filters	= array();
	$sql_select	= "select * from tblproduct_filter where iCategoryId=$idCat and iParentId=0 order by iOrder";
	
	$rs=$dbsql->query($sql_select);
	while($result=$dbsql->fetch_array($rs))
	{
		$filters[]=$result;
	}
	return $filters;
}

function ListChildFilter($idP)
{
global $dbsql,$language;
	
	$filters	= array();
	$sql_select	= "select * from tblproduct_filter where iParentId=$idP order by iOrder";
	
	$rs=$dbsql->query($sql_select);
	while($result=$dbsql->fetch_array($rs))
	{
		$filters[]=$result;
	}
	return $filters;
}

function LoadFilter($id)
{
global $dbsql,$language;
	
	$sql_select	= "select * from tblproduct_filter where iId=$id and iParentId=0";
	
	$rs=$dbsql->query($sql_select);
	$result=$dbsql->fetch_array($rs);
	return $result;
}
function ListOptionFilterValue($idP,$select=0)
{
global $dbsql,$language;
	$options	="<option value=-1>Select</option>";
	$sql_select="select iId,cName,cValue from tblproduct_filter where iParentId=$idP order by iOrder";
	$rs=$dbsql->query($sql_select);
	while($result=$dbsql->fetch_array($rs))
	{
		$values=explode("|",$result['cValue']);		
		if(in_array($select,$values) && $select>0)
		{
			$options.="<option value='".$result['iId']."' selected>".$result['cName']."</option>";
		}else
		{
			$options.="<option value='".$result['iId']."'>".$result['cName']."</option>";
		}
	}
	return $options;
}

function Update_FiltersProduct($filters,$id)
{
global $dbsql,$language;

	foreach($filters as $i => $idFilter)
	{
		if(intval($idFilter)==0)continue;
		$sql_select="select cValue from tblproduct_filter where iId=$idFilter";
		$rs=$dbsql->query($sql_select);
		$result=$dbsql->fetch_array($rs);
		$values=explode("|",$result['cValue']);
		if(!in_array($id,$values))
		{
			$value=$result['cValue'].$id."|";
			$sql_update="update tblproduct_filter set cValue='$value' where iId=$idFilter";
			$dbsql->query($sql_update);
		}
	}	
}

function ListRootCatProduct($idParent=0,$padding=0)
{
global $id,$language,$dbsql,$module;
	$idParent  = intval($idParent);
    $return    = '';
	//-----------------------------------------------------
	$sql_select = "select cName,iId from tblproduct_category where  iParentId=$idParent ";    
	$sql_select.=" and cLanguage='$language' order by iOrder asc";
	$rs=$dbsql->query($sql_select);
	while($result = $dbsql->fetch_array($rs))
	{
		$child= ListRootCatProduct($result["iId"],$padding+20);
		$return .="<div  style='padding-left:".$padding."px;'";
		if($result["iId"] == $id)
		{
			$return .= " class='selected'";
		}
		$return.="><a href='?";
		$return.="module=".$module."&";
		$return.="id=".$result["iId"]."'>";
		$return.="&raquo;".$result["cName"]."</a></div>";
		$return.=$child;
	}
	return $return;
}

function ListCatProductByParentId($idCat,$status=-1)
{
    global $dbsql,$language;
	$idCat         = intval($idCat);
	$sql_where     = " from tblproduct_category where iParentId=$idCat and cLanguage='$language' ";
    if($status>-1)
    {
        $sql_where.= " and iStatus = $status ";
    }

    $sql_select    = "select count(iId) as count ".$sql_where;
	$result        = $dbsql->query_fetch($sql_select);
	$count_all	   = $result['count'];
	list($limit_st,$limit_le)=Slit_Page($count_all);
	//-----------------------------------------------------
	$sql_select = "select * ".$sql_where." order by iOrder limit $limit_st,$limit_le";
	return $dbsql->load($sql_select);
}

function hasChildProductCat($idp)
{
      global $dbsql;
      $idp=intval($idp);
      //-----------------------------------------------------
      $sql_select = "select iId from tblproduct_category where  iParentId = $idp";

      $rs=$dbsql->query($sql_select);
      $rows = $dbsql->num_rows($rs);
      if($rows>0)return true;
      return false;
}
?>